﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Diagnostics;
using BrainTechLLC;
using BrainTechLLC.WPF;
using BrainTechLLC.ThreadSafeObjects;

namespace BrainTechLLC.FolderExplorer
{
    /// <summary>
    /// Interaction logic for ShowFilesListView.xaml
    /// </summary>
    public partial class ShowFilesListView : UserControl
    {
        FileViewModel _ClickedFile;

        public static readonly DependencyProperty FolderProperty =
                   DependencyProperty.Register("Folder", typeof(FolderViewModel), typeof(ShowFilesListView), new PropertyMetadata(new FolderViewModel(null, null), OnFilePropertyChanged));

        public FolderViewModel Folder
        {
            get { return (FolderViewModel)GetValue(FolderProperty); }
            set { SetValue(FolderProperty, value); }
        }

        private static void OnFilePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ShowFilesListView source = d as ShowFilesListView;
            source.UpdateFolder(e.NewValue as FolderViewModel, e.OldValue as FolderViewModel);
        }

        private void UpdateFolder(FolderViewModel f, FolderViewModel old)
        {
            this.topGrid.DataContext = f;
            UpdateUI();
        }

        public static readonly DependencyProperty TextSizeProperty =
            DependencyProperty.Register("TextSize", typeof(double), typeof(ShowFilesListView), new PropertyMetadata(9.0, OnTextSizePropertyChanged));

        public double TextSize
        {
            get { return (double)GetValue(TextSizeProperty); }
            set { SetValue(TextSizeProperty, value); }
        }

        private static void OnTextSizePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ShowFilesListView source = d as ShowFilesListView;
            double v = (double)e.NewValue;
            source.UpdateTextSize(v);
        }

        private void UpdateTextSize(double val)
        {
            UpdateUI();
        }

        public void UpdateUI()
        {
            listView2.SetValue(TextBlock.FontSizeProperty, TextSize);
        }

        public ShowFilesListView()
        {
            InitializeComponent();
        }

        private void mnuOpen_Click(object sender, RoutedEventArgs e)
        {
            FileViewModel file = _ClickedFile;
            
            string directoryName = System.IO.Path.GetDirectoryName(file.File.FullName);
            ProcessStartInfo psi = new ProcessStartInfo()
            {
                UseShellExecute = true,
                WorkingDirectory = directoryName,
                FileName = file.File.FullName,
                WindowStyle = ProcessWindowStyle.Normal,
            };
            Process p = new Process()
            {
                StartInfo = psi,
            };
            p.Start();
        }

        private void mnuOpenContaining_Click(object sender, RoutedEventArgs e)
        {
            FileViewModel file = _ClickedFile;

            string directoryName = System.IO.Path.GetDirectoryName(file.File.FullName);
            ProcessStartInfo psi = new ProcessStartInfo()
            {
                UseShellExecute = true,
                WorkingDirectory = directoryName,
                FileName = directoryName,
                WindowStyle = ProcessWindowStyle.Normal,
            };
            Process p = new Process()
            {
                StartInfo = psi,
            };
            p.Start();
        }

        public FileViewModel GetFileUnderMouse()
        {
            Point pt = Mouse.GetPosition(listView2);
            IInputElement elem = listView2.InputHitTest(pt);
            DependencyObject dep = elem as DependencyObject;
            while ((dep != null) && !(dep is ListViewItem)) { dep = VisualTreeHelper.GetParent(dep); }
            if (dep == null) return null;
            FileViewModel item = listView2.ItemContainerGenerator.ItemFromContainer(dep) as FileViewModel;
            return item;
        }

        private void listView2_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            FileViewModel file = GetFileUnderMouse();
            if (file == null)
            {
                _ClickedFile = null;
                e.Handled = true;
            }
            else
            {
                _ClickedFile = file;
            }
        }

        public List<FileViewModel> GetSelected()
        {
            return listView2.GetSelected<FileViewModel>();
        }
    }
}
